//
//  OrderChoosePayView.swift
//  LeGou
//
//  Created by lxm on 2022/8/6.
//

import UIKit

class OrderChoosePayView: XMBaseView {

    //MARK: --------------------属性--------------------
    var ckMoney = 0.9
    
    var normalMoney = 0.0
    
    var disposeBag = DisposeBag()
    
    var selectType = ZMUtil.fastNilBehavior(type: EnumPayTool.self)
    
    var onGetwalleted : (()->())?
    //MARK: --------------------UI控件--------------------
    @IBOutlet weak var content: UIView!

    @IBOutlet weak var wechat: UIButton!
    
    @IBOutlet weak var zfb: UIButton!
    
    @IBOutlet weak var normal: UIButton!
    
    @IBOutlet weak var vipBtn: UIButton!
    
    @IBOutlet weak var price: UILabel!
    // 支付事件
    var onPayCallback : ((_ tool : EnumPayTool) -> ())?
    
    @IBOutlet weak var viplabel: UILabel!
    
    @IBOutlet weak var normalLabel: UILabel!
    
    @IBOutlet weak var normalWallet: UIView!
    
    @IBOutlet weak var sureBtn: UIButton!
    
    @IBOutlet weak var alipayContent: UIView!
    
    @IBOutlet weak var ckContent: UIView!
    
    @IBOutlet weak var normalContent: UIView!
    
    let loading = XMLoadingView()
    
    var payMoney : Double = 0.0 {
        didSet {
            checkState()
        }
    }
    @IBOutlet weak var wxContent: UIView!
    
    //MARK: --------------------生命周期--------------------
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
        bindAction()
        addConstraints()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        setUI()
        bindAction()
        addConstraints()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        ZMUtil.clipCircle(view: content, Corner: [.topLeft, .topRight], radius: 12)
    }

    //MARK: --------------------UI--------------------
    func setUI() {
        getwallet()
        self.ckContent.isHidden = true
        self.normalContent.isHidden = true
        self.alipayContent.isHidden = true
        self.wxContent.isHidden = true
    }
    
    func addConstraints() {
     
    }
    
    //MARK: --------------------methods--------------------
    func bindAction() {
        selectType.subscribe(onNext:{ [unowned self] resp in
            guard let resp = resp else {
                self.wechat.isSelected = false
                self.vipBtn.isSelected = false
                self.zfb.isSelected    = false
                self.normal.isSelected = false
                return
            }
            self.wechat.isSelected = resp == .wechat
            self.vipBtn.isSelected = resp == .ckPay
            self.zfb.isSelected    = resp == .alipay
            self.normal.isSelected = resp == .balancePay
        }).disposed(by: disposeBag)
    }
    
    func getwallet() {
        loading.showInView(fatherView: content)
        Observable.combineLatest(MineAPI.userBanlance.rxRequest(type: [String : Any].self), CommonAPI.paySetting.rxRequest(type: CommonModels.PaySetting.self)).subscribe(onNext:{ [unowned self] resp in
            self.loading.hideView(anmation: true)
            self.ckMoney = JSON(resp.0)["mallMoney"].doubleValue
            self.normalMoney = JSON(resp.0)["money"].doubleValue
            self.viplabel.text = "创客钱包(¥\(self.ckMoney.zm.toString(keep: 2)))"
            self.normalLabel.text = "普通钱包(¥\(self.normalMoney.zm.toString(keep: 2)))"
            self.wxContent.isHidden = resp.1.payWeixinOpen == 0
            self.alipayContent.isHidden = resp.1.aliPayStatus == 0
            self.ckContent.isHidden = resp.1.ckOpenStatus == 0
            self.normalContent.isHidden = resp.1.balanceOpenStatus == 0
            self.checkState()
            if let onGetwalleted = self.onGetwalleted {
                onGetwalleted()
            }
        }, onError: { e in
            self.loading.showError(e.localizedDescription)
        }).disposed(by: disposeBag)
        
    }
    
    @IBAction func onPayTool(_ sender: UIButton) {
        let index = sender.tag - 100
        guard let state = EnumPayTool.init(rawValue: index) else {
            return
        }
        self.selectType.accept(state)
    }
    
    
    @IBAction func onPay(_ sender: Any) {
        guard let selectType = selectType.value else {
            ZMToast.showToast(title: "请选择一种支付方式")
            return
        }
        if let onPayCallback = onPayCallback {
            onPayCallback(selectType)
        }
    }
    
    @IBAction func onClose(_ sender: Any) {
        removeFromSuperview()
    }
    
    func checkState() {
        vipBtn.isEnabled = ckMoney >= payMoney
        normal.isEnabled = normalMoney >= payMoney
    }
    
    //MARK: --------------------delegate--------------------

}


class CanDisableButton : UIButton {
    var selectImage = "share_gou"
    
    var normalImage = "gou_kong"
    
    var disableImage = "gou_disable"
    
}
